/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bbcnet.awesomeplayer;

import com.phidgets.PhidgetException;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author esingn
 */
public class PlaylistSafer extends JFrame implements ActionListener {
    XML xml = XML.getInstance();
    
    JTextPane jtp_name = new JTextPane();
    JTextField jtf_name = new JTextField();
    JTextPane jtp_safe = new JTextPane();
    JButton jb_user_pw = new JButton("Username / Passwort");
    JButton jb_rfid_pw = new JButton("RFID / Passwort");
    JPanel jp_oben = new JPanel();
    JPanel jp_unten = new JPanel();
    JTextPane clear = new JTextPane();
    JButton jb_ok;
    JButton jb_abb;
    JFrame jf_new;
    JTextPane jtp_user = new JTextPane();
    JTextPane jtp_pw = new JTextPane();
    JTextField jtf_username = new JTextField();
    JPasswordField jtf_pwd = new JPasswordField();
    RFID rfid = RFID.getInstance(jtf_username);
    Gui g;
    boolean isSafer;
    
    public PlaylistSafer(Gui g,boolean a) {
        super("Auth.");
        this.isSafer = a;
        this.g = g;
        
        if(a==true){
            //Objekte zum oberen JPanel werden formatiert das es mir gefält, muhahah
            jp_oben.setLayout(new FlowLayout());
            jtp_name.setText("Playlist name: ");
            jtp_name.setEditable(false);
            jtp_name.setBackground(null);
            jp_oben.add(jtp_name);
            jp_oben.add(jtf_name);
            isSafer = a;
            //genau das gleiche mit dem unterem JPanel
            jtf_name.setPreferredSize(new Dimension(100, 18));
            jp_unten.setLayout(new GridLayout(2, 2));
            jtp_safe.setText("Wie ist die Playlist zu sichern");
            jtp_safe.setEditable(false);
            jtp_safe.setBackground(null);
            jb_user_pw.setText("Username / Passwort");
            jb_rfid_pw.setText("RFID / Passwort");
            jb_user_pw.addActionListener(this);
            jb_rfid_pw.addActionListener(this);
            clear.setEditable(false);
            clear.setBackground(null);
            jp_unten.add(jtp_safe);
            jp_unten.add(clear);
            jp_unten.add(jb_user_pw);
            jp_unten.add(jb_rfid_pw);

            setLayout(new GridLayout(2, 1));
            add(jp_oben);
            add(jp_unten);
            setVisible(true);
            setSize(400, 150);
            this.setResizable(false);
        }else{
            rfid.setPenis(jtf_username);
            newFrame(true);
        }
    }

    public void newFrame(boolean a) {
        jf_new = new JFrame("Authentifizieren");
        JPanel jp_auth = new JPanel();
        JPanel jp_ok_abb = new JPanel();
        jb_ok = new JButton();
        jb_abb = new JButton();



        jf_new.setLayout(new GridLayout(2, 1));
        jp_auth.setLayout(new GridLayout(2, 2));
        jp_ok_abb.setLayout(new FlowLayout());
        jp_auth.add(jtp_name);
        jp_auth.add(jtf_username);
        jp_auth.add(jtp_pw);
        jp_auth.add(jtf_pwd);
        jp_ok_abb.add(jb_ok);
        jp_ok_abb.add(jb_abb);

        jtp_name.setEditable(false);
        jtp_name.setBackground(null);
        jtp_pw.setEditable(false);
        jtp_pw.setBackground(null);
        jb_ok.addActionListener(this);
        jb_abb.addActionListener(this);
        jb_ok.setText("Weiter");
        jb_abb.setText("Abbrechen");
        jtp_name.setText("Identität");
        jtp_pw.setText("Passwort");
        jtf_username.setEditable(a);
        jtf_username.setText("");
        jf_new.add(jp_auth);
        jf_new.add(jp_ok_abb);
        jf_new.setSize(300, 150);
        jf_new.setResizable(false);
        
        jf_new.setVisible(true);

    }
    public boolean checkIfGood(String a){
        if(a.toLowerCase().equals("musik") || xml.isLocked(a)){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        try {
            //rfid = new RFID();
            XML xml = XML.getInstance();
            if (e.getSource().equals(jb_rfid_pw)) {
                if (xml.playlistExists(jtf_name.getText()) && jtf_name.getText().length() > 0 && checkIfGood(jtf_name.getText())) {
                    dispose();
                    newFrame(false);
                }
            }else if(e.getSource().equals(jb_user_pw)){
                if (xml.playlistExists(jtf_name.getText()) && jtf_name.getText().length() > 0 && checkIfGood(jtf_name.getText())) {
                    dispose();
                    newFrame(true);
                }
            } else if (e.getSource().equals(jb_abb)) {
                jf_new.dispose();
            } else if (e.getSource().equals(jb_ok)) {
                if(isSafer){
                    if (!xml.isLocked(jtf_name.getText())) {
                        xml.lockPlaylist(jtf_username.getText(), jtf_pwd.getText(), jtf_name.getText(), false);
                        g.updatePlaylistList();
                        jf_new.dispose();
                    }
                }else{
                    boolean right = xml.attemptAuth(jtf_username.getText(), jtf_pwd.getText(), false);
                    System.out.println(right);
                    if(right){
                        g.login(jtf_username.getText());
                    }
                    jf_new.dispose();
                }
            } 
        } catch (Exception ex) {
                
        }    
    }
}
